JS_SOURCES_DIR=js_sources
VUE_JS_SOURCES_DIR=vue_sources
JS_TEMPLATE_MAIN_FILE=caerp/static/js/template.js
JS_TEMPLATE_DEST=caerp/static/js/templates
JS_TEMPLATES_SOURCE=$(JS_SOURCES_DIR)/src/handlebars
DIRECTORIES=$(shell cd $(JS_TEMPLATES_SOURCE) && find *  -maxdepth 1 -type d)
export PATH := $(JS_SOURCES_DIR)/node_modules/handlebars/bin:$(PATH)


SASSC=$(shell which pysassc)
CSS_SOURCES=css_sources/
CSS_DEST=caerp/static/css/
CSSDIRECTORIES=$(shell cd $(CSS_SOURCES) && find * -maxdepth 0 -type d -not -path "_*")

# Used to build templates used in inline javascript stuff (not webpacked)
js:
	echo $(PATH)
	handlebars $(JS_TEMPLATES_SOURCE)/*.mustache  -f $(JS_TEMPLATE_MAIN_FILE)
	for dir in $(DIRECTORIES);do \
		handlebars $(JS_TEMPLATES_SOURCE)/$$dir/*.mustache -f $(JS_TEMPLATE_DEST)/$$dir.js; \
	done

black:
	black .

# build Marionette/BB js with webpack (bare-metal variant)
prodjs:  # *.minjs
	cd $(JS_SOURCES_DIR) && npm run prod

devjs:  # *.js
	cd $(JS_SOURCES_DIR) && npm run dev

devjs_watch:  # watch files in js_sources and run build automatically on file edit
	cd $(JS_SOURCES_DIR) && npm run devwatch

# build Vuejs js with webpack (bare-metal variant)
prodjs2:  # *.minjs
	cd $(VUE_JS_SOURCES_DIR) && npm run prod

devjs2:  # *.js
	cd $(VUE_JS_SOURCES_DIR) && npm run dev

devjs2_watch:  # watch files in js_sources and run build automatically on file edit
	cd $(VUE_JS_SOURCES_DIR) && npm run devwatch


# build Marionette/BB js with webpack (docker-compose variant)
devjs_dc:
	docker-compose -f js-docker-compose.yaml run --rm webpack-marionette npm run dev

prodjs_dc:
	docker-compose -f js-docker-compose.yaml run --rm webpack-marionette npm run prod

# build VueJS js with webpack (docker-compose variant)
devjs2_dc:
	docker-compose -f js-docker-compose.yaml run --rm webpack-vue npm run dev

prodjs2_dc:
	docker-compose -f js-docker-compose.yaml run --rm webpack-vue npm run prod



# build css with libsassc
css:
	cd ${CSS_SOURCES} && boussole compile

css_watch:
	cd ${CSS_SOURCES} && boussole compile && boussole watch

dev_serve:
	pserve --reload development.ini

dev_db_serve:
	docker-compose -f db-docker-compose.yaml up

dev_db_start:
	docker-compose -f db-docker-compose.yaml up -d

dev_db_stop:
	docker-compose -f db-docker-compose.yaml stop

dev_db_clear: dev_db_stop
	sudo rm -rf mariadb

npm_upgrade:  # Upgrade JS dependencies if required (bare metal variant)
	npm --prefix $(JS_SOURCES_DIR) install
	npm --prefix $(VUE_JS_SOURCES_DIR) install

pip_dev_upgrade:  # Upgrade Python dependencies if required (and version number)
	pip install -e .[dev]

postupgrade_database:
	caerp-migrate development.ini upgrade  # run alembic migrations
	caerp-admin development.ini syncdb  # initialize some database objects


postupgrade_dev: pip_dev_upgrade  devjs_dc prodjs_dc devjs2_dc prodjs2_dc postupgrade_database
postupgrade_dev_legacy: npm_upgrade pip_dev_upgrade  prodjs devjs prodjs2 devjs2 postupgrade_database